# -*- encoding: utf-8 -*-
from django.test import TestCase
from django.test.client import Client
from api.models import Voto, Tema
import json

# Create your tests here.
class TemaTest(TestCase):
    def setUp(self):
        super(TemaTest, self).setUp()
        self.client = Client()
    def test_post(self):
        transaction_post = '{"titulo": "ed", "academico":"Gazmuri", "uai": "Ingeniería", "descripcion": "Modelos estocasticos aplicado a la sociedad", "autor": "Jia", "area": "educacion"}'
        post_response = self.client.post('/api/tema/', transaction_post, content_type='application/json')
        self.assertEqual(post_response.status_code, 201)
        get_response = self.client.get("/api/tema/")
        print get_response.content
    def test_post_with_fecha(self):
        transaction_post = '{"titulo": "ed", "fecha":"2014-05-18T18:50:09.545857", "academico":"Gazmuri", "uai": "Ingenieria", "descripcion": "Modelos estocasticos aplicado a la sociedad", "autor": "Jia", "area": "educacion"}'
        post_response = self.client.post('/api/tema/', transaction_post, content_type='application/json')
        self.assertEqual(post_response.status_code, 201)
        get_response = self.client.get("/api/tema/")
    def test_post_missing_academico(self):
        l = {"titulo": "ed", 'uai': 'Ingenieria', 'descripcion': 'Modelos estocasticos aplicado a la sociedad', 'autor': 'Jia', 'area': 'educacion'}
        post_response = self.client.post('/api/tema/', json.dumps(l), content_type='application/json')
        self.assertEqual(post_response.status_code, 201)
    def test_num_votos(self):
        transaction_post = '{"titulo": "ed", "fecha":"2014-05-18T18:50:09.545857", "academico":"<Gazmuri>", "uai": "Ingenieria", "descripcion": "Modelos estocasticos aplicado a la sociedad", "autor": "Jia", "area": "educacion"}'
        post_response = self.client.post('/api/tema/', transaction_post, content_type='application/json')

        transaction_post = json.dumps({'na': '10654321','tema': '/api/voto/1/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')

        get_response = self.client.get('/api/tema/1/')
        j=json.loads(get_response.content)
        self.assertEqual(j.get('votos',None),1)

        transaction_post = json.dumps({'na': '10654320','tema': '/api/voto/1/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')

        get_response = self.client.get('/api/tema/1/')
        j=json.loads(get_response.content)
        self.assertEqual(j.get('votos',None),2)
    def test_post_valid_invalid_tema(self):
        l = {"titulo": "ed", "fecha":"2014-05-18T18:50:09.545857", "academico":"Gazmuri", "uai": "Ingenieria", "descripcion": "Modelos estocasticos aplicado a la sociedad", "autor": "Jia", "area": "educacion"}
        temas = ['educacion', 'economia y desempleo', 'energia y medio ambiente', 'justicia y seguridad publica', 'pobreza y desigualdad', 'sp y modernizacion', 'salud', 'vivienda y urbanismo', 'derechos civiles', 'otro']
        for t in temas:
            l["area"] = t
            post_response = self.client.post('/api/tema/', json.dumps(l), content_type='application/json')
            self.assertEqual(post_response.status_code, 201)
        l["area"] = "area_inexistente"
        post_response = self.client.post('/api/tema/', json.dumps(l), content_type='application/json')
        self.assertEqual(post_response.status_code, 400)



#TestDoubleVote and check response
#Test voto con na invalido

class VotoTest(TestCase):
    def setUp(self):
        super(VotoTest,self).setUp()
        self.client = Client()
        tm = Tema(titulo="ed",area="Salud",descripcion="Algun tema de salud",autor="Jia",uai="Proyecta UC")
        tm.save()
        v = Voto(na="10633553",tema=tm)
        v.save()
    def test_post_voto(self):
        transaction_post = json.dumps({'na': '10654321','tema': '/api/tema/1/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')
        print post_response.content
        self.assertEqual(post_response.status_code, 201)
    def test_post_duplicate_na(self):
        transaction_post = json.dumps({'na': '10633553','tema': '/api/voto/1/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')
        self.assertEqual(post_response.status_code, 400)
    def test_post_missing_na(self):
        transaction_post = json.dumps({'tema': '/api/voto/1/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')
        self.assertEqual(post_response.status_code, 400)
    def test_post_invalid_na(self):
        transaction_post = json.dumps({'na': 'invalido','tema': '/api/voto/1/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')
        self.assertEqual(post_response.status_code, 400)
    def test_post_missing_tema(self):
        transaction_post = json.dumps({'na': '10654654'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')
        self.assertEqual(post_response.status_code, 400)
    def test_post_inexistent_tema(self):
        transaction_post = json.dumps({'na': '10654321','tema': '/api/voto/10/'})
        post_response = self.client.post('/api/voto/',transaction_post,content_type='application/json')
        self.assertEqual(post_response.status_code, 400)
